'''
Created on 14 jan 2010

@author: Marcus
'''

import unittest
import imp
import sys
import traceback

from svtparser import *
from utility import ContentFactory

class Test(unittest.TestCase):
    
    def _testSVTFactory(self):
        
        results = ContentFactory.getContents()
        self.failIf(results == None or len(results) == 0, "No matches from start page categories")
        
        results = ContentFactory.getContents(results[1].URI)
        self.failIf(results == None or len(results) == 0, "No matches from first subpage")
        
    def _testStartPageParser(self):
    
        parser = StartPageParser()
        results = parser.parse()
        self.failIf(results == None or len(results) == 0, "No matches from startpage")
        
    def _testStartPage(self):
        
        #parser = ProgramPartsParser()
        parser = CategoryProgramsParser()
        
        results = parser.parse("http://svtplay.se/")
        self.failIf(results == None or len(results) == 0, "No matches from startpage")
        
    
    def _testStartPageSubCats(self):
        
        parser = StartPageParser()
        results = parser.parse()
        self.failIf(results == None or len(results) == 0, "No matches from startpage")
        
        pos =0
        
        for item in results:
            pos += 1            
            if pos <= 2:
                continue
            
            self.parseSubItems(ContentFactory.getParser(item.URI))
            
            
            
    def testVideoParse2(self):
        
        parser = VideoParser()
        results = parser.parse("http://svtplay.se/v/1880958/andra_avenyn/del_175?cb,a1364145,1,f,-1/pb,a1364142,1,f,-1/pl,v,,1880958/sb,p103560,1,f,-1")
        
        self.failIf(results == None or len(results) == 0, "No video matches")
        
        
    def _testVideoParse(self):
        
        parser = VideoParser()
        results = parser.parse("http://svtplay.se/v/1911140/rapport/1_3_19_30?cb,a1366518,1,f,-1/pb,a1366516,1,f,-1/pl,v,,1892306/sb,p103263,1,f,-1")
        
        self.failIf(results == None or len(results) == 0, "No video matches")
        
    def _testRapportPrograms(self):
        
        parser = ProgramPartsParser()
        results = parser.parse("http://svtplay.se/t/103261/rapport?cb,a1366518,1,f,-1/pb,a1366516,1,f,-1/pl,v,,1885470/sb,p103263,1,f,")
        
        subResults = ContentFactory.getContents(results[0].URI)
        
        video = ContentFactory.getContents(subResults[0].URI)
        
        
        
        self.failIf(results == None or len(results) == 0, "No matches from Rapport")       
        self.failIf(subResults == None or len(subResults) == 0, "No matches from Rapport 'Rapport 1930'")
        
    
    def onProgress(self, sender, value, text1=None):    
        print "%s \"%s\"" % (value, text1)
        
    def _testListener(self):
        
        parser = StartPageParser()
        
        parser.ProgressEvent.addListener( self.onProgress )
        
        parser.parse()
        
        
    def _testCategoryParse(self):
        
        parser = CategoryParser()
        
        results = parser.parse("http://svtplay.se/kategorier")
        self.failIf(results == None or len(results) == 0, "No matches from categorypage")
    
    def _testABCProgramParts(self):
        
        parser = ProgramPartsParser()
        results = parser.parse("http://svtplay.se/t/103285/abc")
        
        self.failIf(results == None or len(results) == 0, "No matches from categorypage")
        
    
    def _testAll(self):
        self.parseSubItems(ContentFactory.getParser())
        
    def parseSubItems(self, parser):
        
        print "parsing %s" % parser
        
        try:
            results = parser.parse()
        except utility.ParserError, e:
            print "========================================================"  
            print " Error at %s " % parser.URL
            print " %s" % self.formatExceptionInfo()
            print "--------------------------------------------------------" 
        
        for item in results:
            if(item.URI and type(item) is not utility.VideoItem ):
                self.parseSubItems(
                                    ContentFactory.getParser(item.URI) 
                                   )
    
    def formatExceptionInfo(self, level = 6):
        error_type, error_value, trbk = sys.exc_info()
        tb_list = traceback.format_tb(trbk, level)    
        s = "Error: %s \nDescription: %s \nTraceback:" % (error_type.__name__, error_value)
        for i in tb_list:
            s += "\n" + i
        return s    
    
    def __testStartPageCategories(self):
        startPageParser = StartPageParser()
        
        results = startPageParser.parse(SVTConfig.BaseUrl)
        
        self.failIf(results == None or len(results) == 0, "No matches from start page categories")
        self.failIf(len(results) != 7, "Theres supposed to be four categories, found %d" % len(results) )


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()